package tech.aistar.prj.controller;

import tech.aistar.prj.model.dto.VideoDTO;
import tech.aistar.prj.model.dto.VideoQueryDTO;
import tech.aistar.prj.model.pojo.User;
import tech.aistar.prj.model.pojo.Video;
import tech.aistar.prj.service.IVideoService;
import tech.aistar.prj.service.impl.VideoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@WebServlet(urlPatterns = "/video/*")
public class IndexController extends HttpServlet {
    private IVideoService videoService = new VideoServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理一下中文乱码问题 - post
        //req.setCharacterEncoding("utf-8");
        //从session作用域获取
        User user = (User) req.getSession().getAttribute("user");

        String pathInfo = req.getPathInfo();
        switch (pathInfo){
            case "/index":
                //List<VideoDTO> videoList = videoService.getAll();
                //TODO...
                // 多重组合条件查询的
                VideoQueryDTO queryDTO = new VideoQueryDTO();
                //可能传入条件过来
                String title = req.getParameter("title");
                queryDTO.setTitle(title==null?title:title.trim());

                String likes = req.getParameter("likes");
                //此处为什么还需非空判断,需要进行类型的转换
                if(null!=likes && likes.trim().length()>0){
                    queryDTO.setLikes(Integer.valueOf(likes));
                }
                String score = req.getParameter("score");
                if(null!=score && score.trim().length()>0){
                    queryDTO.setScore(Double.valueOf(score));
                }
                String username = req.getParameter("username");
                queryDTO.setUsername(username==null?username:username.trim());

                List<VideoDTO> videoList = videoService.findMoreConditional(queryDTO);

                //升序...
                String sort = req.getParameter("sort");
                if("1".equals(sort)){
                    videoList.sort(Comparator.comparing(e-> e.getVideo().getLikes()));
                }else if("2".equals(sort)){
                    videoList.sort(Comparator.comparing(e-> e.getVideo().getLikes()));
                    Collections.reverse(videoList);
                }


                //数据回显的功能
                //第一种方式 - 散列的传
                //req.setAttribute("username",username==null?"":username.trim());
                //req.setAttribute("title",title==null?"":title.trim());
                //req.setAttribute("likes",likes==null?"":likes.trim());
                //req.setAttribute("score",score==null?"":score.trim());

                Map<String,String> dataMap = new HashMap<>();
                dataMap.put("username",username==null?"":username.trim());
                dataMap.put("title",title==null?"":title.trim());
                dataMap.put("likes",likes==null?"":likes.trim());
                dataMap.put("score",score==null?"":score.trim());

                req.setAttribute("dataMap",dataMap);


                //把这个数据放入到request作用域
                req.setAttribute("videoList",videoList);
                //转发到视图的servlet => 使用Jsp替代.
                //req.getRequestDispatcher("/video/indexView").forward(req,resp);

                req.getRequestDispatcher("/nav/videoView").forward(req,resp);
                break;
            case "/personal":
                List<Video> videos = videoService.searchByUser(user.getUsername());
                req.setAttribute("videos",videos);
                //转发到我的发布的视频页面
                req.getRequestDispatcher("/nav/personalView").forward(req,resp);
                break;

            case "/release":
                //获取标题
                String t = req.getParameter("title");
                String videoUrl = req.getParameter("videoUrl");
                String like = req.getParameter("likes");
                String scores = req.getParameter("score");
                String status = req.getParameter("status");
                Video v = new Video();
                v.setTitle(t);
                v.setVideoUrl(videoUrl);
                v.setLikes(Integer.valueOf(like));
                v.setScore(Double.valueOf(scores));
                v.setStatus(Integer.valueOf(status));
                v.setUserId(user.getId());

                v.setCreateDate(new Date());
                videoService.release(v);
                //一旦保存成功之后 - 跳转到我的发布中去
                resp.sendRedirect("/st/video/personal");
                break;
            case "/change":
                //未发布->发布的状态
                String id = req.getParameter("id");
                videoService.changeStatus(Integer.valueOf(id),user.getId(),0);

                resp.sendRedirect("/st/video/personal");
                break;
            case "/del":
                //删除 - 物理删除
                //status=1的是不会展示的...
                String ids = req.getParameter("id");
                videoService.changeStatus(Integer.valueOf(ids),user.getId(),1);

                resp.sendRedirect("/st/video/personal");
                break;
            case "/updateGet":
                String vId = req.getParameter("id");
                Video vd = videoService.getById(Integer.valueOf(vId));

                //将它放入到request作用域中去
                req.setAttribute("vd",vd);
                req.getRequestDispatcher("/nav/updateView").forward(req,resp);
                break;
            case "/update":

                String vid = req.getParameter("id");
                Video dv = videoService.getById(Integer.valueOf(vid));

                String tt = req.getParameter("title");
                String vUrl = req.getParameter("videoUrl");
                String sta = req.getParameter("status");

                dv.setTitle(tt);
                dv.setVideoUrl(vUrl);
                dv.setStatus(Integer.valueOf(sta));

                videoService.updateVideoWithUser(dv);

                resp.sendRedirect("/st/video/personal");

                break;
        }
    }
}
